package com.zhangfei;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.net.URL;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class FileDown {
    /**
     * 下载文件---返回下载后的文件存储路径
     *
     * @param url      文件地址
     * @param dir      存储目录
     * @param fileName 存储文件名
     * @return
     */
    public static void downloadHttpUrl(String url, String dir, String fileName) {
        try {
            URL httpurl = new URL(url);
            File dirfile = new File(dir);
            if (!dirfile.exists()) {
                dirfile.mkdirs();
            }
            FileUtils.copyURLToFile(httpurl, new File(dir + fileName));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        ThreadPoolExecutor importExportTaskThreadPool = new ThreadPoolExecutor(
                2,
                2,
                0L,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(10240),
                r -> new Thread(r, "ImportExportTaskLogicThreadPool-" + r.hashCode()));

        for (int i = 1; i < 100; i++) {

            int finalI = i;
            importExportTaskThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    String fileName=21809+ finalI +".epub";
                    String urlToDownload = "https://sobooks.cloud/2024/04/"+fileName;
                    String saveLocation = "C:/Users/滨湖华为/Desktop/books/"; // Windows路径示例
                    downloadHttpUrl(urlToDownload, saveLocation, fileName);
                }
            });
        }



    }
}
